﻿#pragma once
#include<iostream>
using namespace std;
//枚举类型定义颜色
enum Colour
{
	RED,
	BLACK
};
//节点结构（默认存储pair类型的key/val结构）
template<class K, class V>
struct RBTreeNode
{
	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _parent(nullptr)
		, _left(nullptr)
		, _right(nullptr)
		,_col(RED)
	{}
	pair<K, V> _kv;
	RBTreeNode* _parent;
	RBTreeNode* _left;
	RBTreeNode* _right;
	Colour _col;//初始化为红色
};

//红黑树
template<class K,class V>
class RBTree
{
	typedef RBTreeNode<K,V> Node;
public:
	//默认构造
	RBTree() = default;
	//拷贝构造
	RBTree(const RBTree<K,V>& rbt)
	{
		_root=_copy(rbt._root);
	}
	// 赋值重载
	RBTree<K, V>& operator=(RBTree<K, V> tmp)
	{
		std::swap(_root, tmp._root);
		return *this;
	}
	//中序遍历
	void InOrder()
	{
		_InOrder(_root);
	}
	//二叉树的析构
	~RBTree()
	{
		_Destroy(_root);
	}
	
	//红黑树的查找
	Node* Find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}
	//红黑树的插入
	bool insert(const pair<K, V>& kv)
	{
		//如果树为空，在根插入并且颜色为黑色
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		//树不为空按搜索树规则先进行插入
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kv.first < cur->_kv.first)//小往左走
			{
				parent = cur;
				cur = parent->_left;
			}
			else if (kv.first > cur->_kv.first)//大往右走
			{
				parent = cur;
				cur = parent->_right;
			}
			else
			{
				return false;//不支持相同元素的插入
			}
		}
		cur = new Node(kv);
		cur->_col = RED;
		if (kv.first < parent->_kv.first)//K小插入在左边
			parent->_left = cur;
		else//K大插入在右边
			parent->_right = cur;
		cur->_parent = parent;

		//插入后进行维护红黑树规则的逻辑
		//parent存在且为红
		while (parent&& parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			//p在g的右边
			if (parent == grandfather->_right)
			{
				//g
			//u		p
				Node* uncle = grandfather->_left;
				if (uncle&& uncle->_col == RED)//uncle存在且为红
				{
					//变色处理
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;
					//更新cur继续向上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else//uncle不存在或者存在且为黑
				{
					if (cur == parent->_right)
					{
						//g
					//u		p
					//		   c
					//以g为旋转点进行左单旋
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//g
					//u		p
					//	  c
					//进行右左双旋
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					//旋转+变色后链接祖先节点的节点为黑，必然不会发生连续红色节点的情况直接break;
					break;
				}
			}
			else//p在g的左边
			{
				//g
			//p		u
				Node* uncle = grandfather->_right;
				if (uncle&& uncle->_col == RED)//uncle存在且为红
				{
					//变色处理
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;
					//更新cur继续向上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else//uncle不存在或者存在且为黑
				{
					if (cur == parent->_left)
					{
						//g
					//p		u
				//c
					//以g为旋转点进行右单旋
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//g
					//p		u
				//		c
					//进行左右双旋
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					//旋转+变色后链接祖先节点的节点为黑，必然不会发生连续红色节点的情况直接break;
					break;
				}
			}
		}
		//如果持续更新变色到根
		_root->_col = BLACK;
		return true;
	}
	//检查平衡
	bool IsBalance()
	{
		if (_root == nullptr)
			return true;
		if (_root->_col == RED)
			return false;

		// 参考值 
		int refNum = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}

private:
	//右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		Node* pParent = parent->_parent;

		parent->_left = subLR;
		if (subLR)//如果不为空
			subLR->_parent = parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (pParent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}
			subL->_parent = pParent;
		}
	}
	//左单旋
	void RotateL(Node* parent)
	{
		Node* pParent = parent->_parent;
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		subR->_left = parent;
		parent->_parent = subR;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		if (pParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subR;
			}
			else
			{
				pParent->_right = subR;
			}
			subR->_parent = pParent;
		}
	}
	//检查每条路径的黑色节点是否相等，是否有连续的红色节点
	bool Check(Node* root, int blackNum, const int refNum)
	{
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了 
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在黑色结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}

		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了 
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红色节点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}
	//递归拷贝
	Node* _copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* newNode = new Node(root->_kv);
		newNode->_left = _copy(root->_left);
		newNode->_right = _copy(root->_right);
		return newNode;
	}
	//中序遍历
	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << "<" << root->_kv.first << "," << root->_kv.second << ">" << endl;
		_InOrder(root->_right);
	}
	//二叉树的销毁
	void _Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		_Destroy(root->_left);
		_Destroy(root->_right);
		delete root;
	}

private:
	Node* _root=nullptr;
};


